package cn.anc.aonicardv.util;

import android.util.Log;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.anc.httpcontrolutil.cache.CacheManager;

/**
 * Created by yangdai on 2017/6/27.
 */

public class StringUtils {
    /** 判断字符串是否为空 */
    public static boolean isEmptyOrNull(String str) {
        boolean result = false;
        if ("".equals(str) || null == str) {
            result = true;
        }
        return result;
    }

    public static boolean isNumber(String number) {
        if (number == null) return false;
        return number.matches("[+-]?[1-9]+[0-9]*(\\.[0-9]+)?");
    }

    public static boolean isAlpha(String alpha) {
        if (alpha == null) return false;
        return alpha.matches("[a-zA-Z]+");
    }

    public static boolean isContainNumberAndLetter(String string) {
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (!((c >= 65 && c <= 122) || (c >= 48 && c <= 57))) {
                return false;
            }

        }
        return true;
    }

    public static void  printLog(String tag,String tmpLog){
//        LogUtil.e("llcTest0111","--printLog  "+tag+tmpLog);
        String time = DateUtils.getCurrentDateTime();
        try {
            FileUtils.writeFile(CacheManager.CACHE_ROOT_DIR + "/" + "rtspLog" + ".txt", time+":"+tag+ tmpLog+ "\n", true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean isContainChinese(String str) {

        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;

    }

    public static int convertToInt(String str) throws NumberFormatException {
        int s, e;
        for (s = 0; s < str.length(); s++)
            if (Character.isDigit(str.charAt(s)))
                break;
        for (e = str.length(); e > 0; e--)
            if (Character.isDigit(str.charAt(e - 1)))
                break;
        if (e > s) {
            try {
                return Integer.parseInt(str.substring(s, e));
            } catch (NumberFormatException ex) {
                Log.e("convertToInt", ex.getMessage());
                throw new NumberFormatException();
            }
        } else {
            throw new NumberFormatException();
        }
    }

}
